﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Galoap
{
    /// <summary>
    /// The ImageTagQuestion abstract superclass
    /// </summary>
    public partial class ImageTagQuestion : Question
    {
        public string Help {get; set; } // help data
        public List<StringAnswer> Taboo { get; set; } 
   
        /// <summary>
        /// The constructor of the ImageTagQuestion
        /// </summary>
        public ImageTagQuestion() : base()
        {
            TextBuilder = new QuestionText("Inserisci un tag", "associabile all'immagine", ".");
            Text = TextBuilder.GetQuestionText();
            Help = "Per tag si intende un termine associato a un contenuto digitale, utile per facilitarne l'indicizzazione nei motori di ricerca.";

        }


        /// <summary>
        /// Validate mathod which test if the format of the answer is correct for this question. 
        /// </summary>
        /// <param name="reply">the representation string of the answer</param>
        /// <returns>true if the param is valid, false otherwise</returns>
        public override bool Validate(string reply)
        {
            // verifies if the reply is not a number
            if (reply.Length == 0) return false;
            try
            {
                Convert.ToInt32(reply);
                return false;
            }
            catch (Exception)
            {
                foreach (StringAnswer a in Taboo) if (a.Data.Equals(reply)) return false;
                return true;
            }
        }

        /// <summary>
        /// method which return the number of valid answers given to this question
        /// </summary>
        /// <returns>the number of valid answers</returns>
        public override int GetMostFrequentValidAnswerCount()
        {
            return GetAnswersOrderedByFrequency<StringAnswer>().Except(Taboo).First().Counter;
        }

        /// <summary>
        /// init method to inizialize Question and other features
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            // set taboos
            try
            {
                Taboo = GetAnswersOrderedByFrequency<StringAnswer>().Where(q => q.Counter >= 1).ToList();
            }
            catch (System.InvalidOperationException)
            {
                Taboo = new List<StringAnswer>();
            }
        }
    }

}
